package com.ykx.message.facade.impl;

import java.util.Date;
import java.util.UUID;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.util.StringUtils;

import com.ykx.message.constant.MsgConstant;
import com.ykx.message.facade.MessageFacade;
import com.ykx.message.model.ReqMessage;
import com.ykx.message.model.RspValue;
import com.ykx.message.model.Tmessage;
import com.ykx.message.service.MessageService;

public class MessageFacadeImpl implements MessageFacade{
	private static Logger log = LoggerFactory.getLogger(MessageFacadeImpl.class);

	@Autowired
	private MessageService messageService;
	@Autowired
	private JmsTemplate jmsTemplate;
	
	public RspValue saveMessageNotConfirm(ReqMessage reqMessage) {
		log.info("saveMessageNotConfirm-reqMessage={}",reqMessage);
		RspValue rsp = new RspValue();
		try {
			if(reqMessage==null) {
				rsp.setCode(MsgConstant.param_lose);
				rsp.setMsg(MsgConstant.param_msg);
				log.info("saveMessageNotConfirm-rsp={}",rsp);
				return rsp;
			}
			Tmessage message=new Tmessage();
			message.setAreadlyDead(MsgConstant.areadly_dead_no);
			message.setConsumerQueue(reqMessage.getConsumerQueue());
			message.setEditTime(new Date());
			message.setCreateTime(new Date());
			message.setField1(reqMessage.getField1());
			message.setField2(reqMessage.getField2());
			message.setField3(reqMessage.getField3());
			message.setId(UUID.randomUUID().toString());
			message.setMessageBody(reqMessage.getMessageBody());
			message.setMessageDataType(reqMessage.getMessageDataType());
			message.setMessageId(reqMessage.getMessageId());
			message.setMessageSendTimes(0);
			message.setRemark(reqMessage.getRemark());
			message.setVersion(0);
			message.setStatus(MsgConstant.status_not_send);
			messageService.insert(message);
			rsp.setCode(MsgConstant.success);
			log.info("saveMessageNotConfirm-rsp={}",rsp);
			return rsp;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rsp.setCode(MsgConstant.sys_error);
			rsp.setMsg(MsgConstant.sys_error_msg);
			log.info("saveMessageNotConfirm-rsp={}",rsp);
			return rsp;
		}
	}

	public RspValue confirmSendMessage(String messageId, String consumerQueue) {
		log.info("confirmSendMessage---messageId={},consumerQueue={}",messageId,consumerQueue);
		RspValue rsp = new RspValue();
		try {
			rsp = checkParam(messageId, consumerQueue);
			if(rsp.getCode()!=MsgConstant.success) {
				log.info("confirmSendMessage---rsp={}",rsp);
				return rsp;
			}
			final Tmessage message = messageService.getMessage(messageId, consumerQueue);
			if(message==null) {
				rsp.setCode(MsgConstant.data_notExit);
				rsp.setMsg(MsgConstant.data_notExit_msg);
				log.info("confirmSendMessage---rsp={}",rsp);
				return rsp;
			}
			message.setStatus(MsgConstant.status_sending);
			message.setEditTime(new Date());
			messageService.update(message);
			//发送消息到MQ
			jmsTemplate.setDefaultDestinationName(message.getConsumerQueue());
			jmsTemplate.send(new MessageCreator() {
				public Message createMessage(Session session) throws JMSException {
					return session.createTextMessage(message.getMessageBody());
				}
			});
			rsp.setCode(MsgConstant.success);
			log.info("confirmSendMessage---rsp={}",rsp);
			return rsp;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rsp.setCode(MsgConstant.sys_error);
			rsp.setMsg(MsgConstant.sys_error_msg);
			log.info("confirmSendMessage---rsp={}",rsp);
			return rsp;
		}
	}

	public RspValue saveMessageConfirmSend(final ReqMessage reqMessage) {
		log.info("saveMessageConfirmSend---reqMessage={}",reqMessage);
		RspValue rsp = new RspValue();
		try {
			if(reqMessage==null) {
				rsp.setCode(MsgConstant.param_lose);
				rsp.setMsg(MsgConstant.param_msg);
				log.info("saveMessageConfirmSend---rsp={}",rsp);
				return rsp;
			}
			Tmessage message=new Tmessage();
			message.setAreadlyDead(MsgConstant.areadly_dead_no);
			message.setConsumerQueue(reqMessage.getConsumerQueue());
			message.setEditTime(new Date());
			message.setCreateTime(new Date());
			message.setField1(reqMessage.getField1());
			message.setField2(reqMessage.getField2());
			message.setField3(reqMessage.getField3());
			message.setId(UUID.randomUUID().toString());
			message.setMessageBody(reqMessage.getMessageBody());
			message.setMessageDataType(reqMessage.getMessageDataType());
			message.setMessageId(reqMessage.getMessageId());
			message.setMessageSendTimes(0);
			message.setRemark(reqMessage.getRemark());
			message.setVersion(0);
			message.setStatus(MsgConstant.status_sending);
			messageService.insert(message);
			//发送消息到MQ
			jmsTemplate.setDefaultDestinationName(message.getConsumerQueue());
			jmsTemplate.send(new MessageCreator() {
				public Message createMessage(Session session) throws JMSException {
					return session.createTextMessage(reqMessage.getMessageBody());
				}
			});
			rsp.setCode(MsgConstant.success);
			log.info("saveMessageConfirmSend---rsp={}",rsp);
			return rsp;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rsp.setCode(MsgConstant.sys_error);
			rsp.setMsg(MsgConstant.sys_error_msg);
			log.info("saveMessageConfirmSend---rsp={}",rsp);
			return rsp;
		}
	}

	public RspValue directSendNotSaveMessage(final ReqMessage reqMessage) {
		log.info("directSendNotSaveMessage---reqMessage={}",reqMessage);
		RspValue rsp = new RspValue();
		//发送消息到MQ
		jmsTemplate.setDefaultDestinationName(reqMessage.getConsumerQueue());
		jmsTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(reqMessage.getMessageBody());
			}
		});
		rsp.setCode(MsgConstant.success);
		return rsp;
	}

	public RspValue reSendMessage(String messageId, String consumerQueue) {
		RspValue rsp = new RspValue();
		rsp = checkParam(messageId, consumerQueue);
		if(rsp.getCode()!=MsgConstant.success) {
			return rsp;
		}
		try {
			final Tmessage message = messageService.getMessage(messageId, consumerQueue);
			if(message==null) {
				rsp.setCode(MsgConstant.data_notExit);
				rsp.setMsg(MsgConstant.data_notExit_msg);
				return rsp;
			}
			message.setMessageSendTimes(message.getMessageSendTimes()+1);
			message.setEditTime(new Date());
			//发送消息到MQ
			jmsTemplate.setDefaultDestinationName(message.getConsumerQueue());
			jmsTemplate.send(new MessageCreator() {
				public Message createMessage(Session session) throws JMSException {
					return session.createTextMessage(message.getMessageBody());
				}
			});
			rsp.setCode(MsgConstant.success);
			return rsp;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rsp.setCode(MsgConstant.sys_error);
			rsp.setMsg(MsgConstant.sys_error_msg);
			return rsp;
		}
	}

	public RspValue setMessageToAreadlyDead(String messageId, String consumerQueue) {
		RspValue rsp = new RspValue();
		try {
			rsp = checkParam(messageId, consumerQueue);
			if(rsp.getCode()!=MsgConstant.success) {
				return rsp;
			}
			Tmessage message = messageService.getMessage(messageId, consumerQueue);
			if(message==null) {
				rsp.setCode(MsgConstant.data_notExit);
				rsp.setMsg(MsgConstant.data_notExit_msg);
				log.info("directSendNotSaveMessage---rsp={}",rsp);
				return rsp;
			}
			message.setAreadlyDead(MsgConstant.areadly_dead_yes);
			message.setEditTime(new Date());
			messageService.update(message);
			rsp.setCode(MsgConstant.success);
			log.info("directSendNotSaveMessage---rsp={}",rsp);
			return rsp;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rsp.setCode(MsgConstant.sys_error);
			rsp.setMsg(MsgConstant.sys_error_msg);
			log.info("directSendNotSaveMessage---rsp={}",rsp);
			return rsp;
		}
	}

	public RspValue getMessage(String messageId, String consumerQueue) {
		log.info("getMessage---messageId={},consumerQueue={}",messageId,consumerQueue);
		RspValue rsp = new RspValue();
		try {
			rsp = checkParam(messageId, consumerQueue);
			if(rsp.getCode()!=MsgConstant.success) {
				log.info("getMessage---rsp={}",rsp);
				return rsp;
			}
			Tmessage message = messageService.getMessage(messageId, consumerQueue);
			if(message==null) {
				rsp.setCode(MsgConstant.data_notExit);
				rsp.setMsg(MsgConstant.data_notExit_msg);
				log.info("getMessage---rsp={}",rsp);
				return rsp;
			}
			rsp.setCode(MsgConstant.success);
			rsp.setObj(message);
			log.info("getMessage---rsp={}",rsp);
			return rsp;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rsp.setCode(MsgConstant.sys_error);
			rsp.setMsg(MsgConstant.sys_error_msg);
			log.info("getMessage---rsp={}",rsp);
			return rsp;
		}
	}

	public RspValue deleteMessage(String messageId, String consumerQueue) {
		log.info("deleteMessage---messageId={},consumerQueue={}",messageId,consumerQueue);
		RspValue rsp = new RspValue();
		try {
			rsp = checkParam(messageId, consumerQueue);
			if(rsp.getCode()!=MsgConstant.success) {
				log.info("deleteMessage---rsp={}",rsp);
				return rsp;
			}
			messageService.remove(messageId, consumerQueue);
			rsp.setCode(MsgConstant.success);
			log.info("deleteMessage---rsp={}",rsp);
			return rsp;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rsp.setCode(MsgConstant.sys_error);
			rsp.setMsg(MsgConstant.sys_error_msg);
			log.info("deleteMessage---rsp={}",rsp);
			return rsp;
		}
	}
	/**
	 * 更新修改时间.发送次数加1.发送消息
	 */
	public RspValue reSendAllDeadMessageByQueueName(String queueName, int batchSize) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	public RspValue queryNotConfirmTimeOut(int batchSize) {
		// TODO Auto-generated method stub
		return null;
	}

	public RspValue queryConfirmTimeOut(int batchSize) {
		// TODO Auto-generated method stub
		return null;
	}

	private RspValue checkParam(String messageId,String consumerQueue) {
		RspValue rsp = new RspValue();
		if(StringUtils.isEmpty(messageId)) {
			rsp.setCode(MsgConstant.messageId_null);
			rsp.setMsg(MsgConstant.messageId_null_msg);
			return rsp;
		}
		if(StringUtils.isEmpty(consumerQueue)) {
			rsp.setCode(MsgConstant.consumerQueue_null);
			rsp.setMsg(MsgConstant.consumerQueue_null_msg);
			return rsp;
		}
		rsp.setCode(MsgConstant.success);
		return rsp;
	}

}
